﻿package com.fendou.virtualphone.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.fendou.virtualphone.bean.CallBeanInfo;
import com.fendou.virtualphone.db.CallPhoneHistoryDao;
import com.fendou.virtualphone.db.DBAdapter;

public class CallPhoneHistoryImpl implements CallPhoneHistoryDao {

	private DBAdapter mdb;
	private Context context;

	public CallPhoneHistoryImpl(Context context) {
		this.context = context;
	}

	@Override
	public void saveCallBean(String uid, CallBeanInfo msg) {
		try {
			mdb = new DBAdapter(this.context);
			mdb.open();
			ContentValues cv = new ContentValues();
			cv.put(mdb.CALLHISTORY_NAME, msg.getName());
			cv.put(mdb.CALLHISTORY_PHONE_NUM, msg.getPhone_num());
			cv.put(mdb.CALLHISTORY_CALL_TIME, msg.getCall_time());
			cv.put(mdb.CALLHISTORY_RING_MODE, msg.getRing_mode());
			cv.put(mdb.CALLHISTORY_CURRENT_CALL, msg.getCurrent_call());
			cv.put(mdb.CALLHISTORY_CALL_TYPE, msg.getCall_type());
			mdb.insertData(mdb.TABLE_CALLHISTORY, cv);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		} finally {
			if (mdb != null) {
				if (mdb.mSQLiteDatabase != null) {
					mdb.mSQLiteDatabase.close();
				}
				mdb.close();
			}
		}
	}

	@Override
	public List<CallBeanInfo> getCallBeanInfos(String callType) {
		// TODO Auto-generated method stub
		List<CallBeanInfo> msgs = new ArrayList<CallBeanInfo>();
		Cursor mCursor = null;
		try {
			mdb = new DBAdapter(this.context);
			mdb.open();
			String sql = "SELECT * FROM " + mdb.TABLE_CALLHISTORY + " WHERE "
					+ mdb.CALLHISTORY_CALL_TYPE + " =? ORDER BY "
					+ mdb.CALLHISTORY_CALL_TIME + " desc ";
			mCursor = mdb.select(sql, new String[] { callType });
			if (mCursor == null || mCursor.getCount() < 1) {
				return msgs;
			}
			for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor
					.moveToNext()) {
				CallBeanInfo msg = new CallBeanInfo();
				msg.setId(mCursor.getInt(mCursor
						.getColumnIndex(mdb.CALLHISTORY_ID)));
				msg.setName(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_NAME)));// 用户名
				msg.setPhone_num(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_PHONE_NUM)));
				msg.setCall_time(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CALL_TIME)));
				msg.setRing_mode(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_RING_MODE)));
				msg.setCurrent_call(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CURRENT_CALL)));
				msg.setCall_type(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CALL_TYPE)));
				msgs.add(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (mCursor != null) {
				mCursor.close();
			}
			if (mdb != null) {
				if (mdb.mSQLiteDatabase != null) {
					mdb.mSQLiteDatabase.close();
				}
				mdb.close();
			}
		}
		return msgs;
	}

	@Override
	public void updateCallHistoryStatus(CallBeanInfo callinfo) {
		try {
			mdb = new DBAdapter(this.context);
			mdb.open();
			// 这里更改所有 ,应该要加限制条件
			String sql = "update " + mdb.TABLE_CALLHISTORY + " set "
					+ mdb.CALLHISTORY_NAME + " = ?,"
					+ mdb.CALLHISTORY_PHONE_NUM + " = ?,"
					+ mdb.CALLHISTORY_CALL_TIME + " = ?,"
					+ mdb.CALLHISTORY_RING_MODE + " = ?,"
					+ mdb.CALLHISTORY_CURRENT_CALL + " = ?,"
					+ mdb.CALLHISTORY_CALL_TYPE + " = ?," + " where "
					+ mdb.CALLHISTORY_ID + "=? ";
			mdb.deleteOrUpdate(
					sql,
					new String[] { callinfo.getName(), callinfo.getPhone_num(),
							callinfo.getCall_time(), callinfo.getRing_mode(),
							callinfo.getCurrent_call(),
							callinfo.getCall_type(),
							String.valueOf(callinfo.getId()) });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (mdb != null) {
				if (mdb.mSQLiteDatabase != null) {
					mdb.mSQLiteDatabase.close();
				}
				mdb.close();
			}
		}
	}

	@Override
	public boolean deleteById(Long callID) {
		try {
			mdb = new DBAdapter(this.context);
			mdb.open();
			String sql = "delete from " + mdb.TABLE_CALLHISTORY + " where "
					+ mdb.CALLHISTORY_ID + " =?";
			mdb.deleteOrUpdate(sql, new Object[] { callID });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (mdb != null) {
				if (mdb.mSQLiteDatabase != null) {
					mdb.mSQLiteDatabase.close();
				}
				mdb.close();
			}
		}
	}

	@Override
	public List<CallBeanInfo> getCallBeanInfos(Object uid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallBeanInfo findCurrentCallBean() {

		CallBeanInfo callBeanInfo = null;

		Cursor mCursor = null;
		try {
			mdb = new DBAdapter(this.context);
			mdb.open();
			String sql = "SELECT * FROM " + mdb.TABLE_CALLHISTORY + " WHERE "
					+ mdb.CALLHISTORY_CURRENT_CALL + " =? ORDER BY "
					+ mdb.CALLHISTORY_CALL_TIME + " desc ";
			mCursor = mdb.select(sql, new String[] { "0" });
			if (mCursor == null || mCursor.getCount() < 1) {
				return null;
			}
			for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor
					.moveToNext()) {
				callBeanInfo = new CallBeanInfo();
				callBeanInfo.setId(mCursor.getInt(mCursor
						.getColumnIndex(mdb.CALLHISTORY_ID)));
				callBeanInfo.setName(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_NAME)));// 用户名
				callBeanInfo.setPhone_num(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_PHONE_NUM)));
				callBeanInfo.setCall_time(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CALL_TIME)));
				callBeanInfo.setRing_mode(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_RING_MODE)));
				callBeanInfo.setCurrent_call(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CURRENT_CALL)));
				callBeanInfo.setCall_type(mCursor.getString(mCursor
						.getColumnIndex(mdb.CALLHISTORY_CALL_TYPE)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (mCursor != null) {
				mCursor.close();
			}
			if (mdb != null) {
				if (mdb.mSQLiteDatabase != null) {
					mdb.mSQLiteDatabase.close();
				}
				mdb.close();
			}
		}
		return callBeanInfo;

	}

}
